<script>
import { mapState } from 'vuex'
import InstitutionSelect from '@/components/institutionSelect/institutionSelect.vue'

export default {
  name: 'deliveryman',
  components: { InstitutionSelect },
  data() {
    return {
      pageParams: {
        // 当前页
        page: 1,
        //  一页多少条
        pageSize: 10,
        // 电话
        phone: null,
        // 司机名称
        name: null,
        // 机构ID
        agentId: null,
        // 司机编号 账号
        account: null
      },
      loading: false,
      placeholder: '请选择所属机构'
    }
  },
  methods: {
    /*
    *获取快递员分页数据的回调函数
    *  */
    async fetchCourierList() {
      try {
        this.loading = true
        await this.$store.dispatch('deliverymanStore/fetchCourierListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /*
    *pageSize发生变化的回调函数
    *  */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchCourierList()

    },
    /*
    *page发生变化的回调函数
    *  */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchCourierList()
    },
    /*
    * 重置按钮的回调函数
    *  */
    handleReset() {
      this.pageParams.page = 1
      this.pageParams.account = null
      this.pageParams.phone = null
      this.pageParams.name = null
      this.pageParams.agentId = null
      this.placeholder = '请选择所属机构'
      this.fetchCourierList()
    },
    /*
    *
    * 搜索按钮的回调函数
    *  */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchCourierList()
    },
    /*
    *
    *  */
    nodeClick({ id, name }) {
      this.pageParams.agentId = id
      this.placeholder = name
    }
  },
  computed: {
    ...mapState('deliverymanStore', ['counts', 'courierList'])
  },
  mounted() {
    this.fetchCourierList()
  }
}
</script>

<template>
  <div class="deliveryman">
    <el-card shadow="never" class="top">
      <el-form inline :model="pageParams">
        <el-form-item label="快递员账号:">
          <el-input placeholder="请输入快递员账号" v-model="pageParams.account"></el-input>
        </el-form-item>
        <el-form-item label="快递员姓名:">
          <el-input placeholder="请输入快递员姓名" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item label="快递员手机号:">
          <el-input placeholder="请输入快递员手机号" v-model="pageParams.phone"></el-input>
        </el-form-item>
        <el-form-item label="所属机构:">
          <institution-select :placeholder="placeholder" @nodeClick="nodeClick"></institution-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" class="bottom" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
      <el-table border stripe :data="courierList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}">
        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
        <el-table-column prop="stationName" label="岗位" align="center"></el-table-column>
        <el-table-column label="快递员头像" align="center" width="100px">
          <template>
            <el-avatar :size="50">
              <img src="@/assets/avatar/default.jpg"/>
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="account" label="快递员账号" align="center"></el-table-column>
        <el-table-column prop="name" label="快递员姓名" align="center"></el-table-column>
        <el-table-column prop="mobile" label="快递员手机号" align="center"></el-table-column>
        <el-table-column label="所属机构" align="center">
          <template #default="{row}">
            {{ row.agency.name }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="工作状态" align="center">
          <template #default="{row}">
            {{ row.workStatus===1?'上班':'休息' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="$router.push({ path:'/scope-del',query:{id:row.userId} })">作业范围分配</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 15, 20, 25]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="counts" background></el-pagination>

    </el-card>
  </div>
</template>

<style scoped lang="scss">
.deliveryman {
  padding: 20px;

  .top {
    .el-form-item {
      width: 380px;

      .el-input {
        width: 130%;
      }
    }
  }

  .bottom {
    margin-top: 20px;

    .el-table {
      margin-bottom: 10px;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
